package com.jkd.analysis.protocol;

import cn.hutool.core.util.StrUtil;
import com.jkd.analysis.alarm.MyAlarm;
import com.jkd.analysis.protocol._interface.EquipmentAgreement;
import com.jkd.taos.domain.SourceData;
import com.jkd.utils.JkConvertUtil;
import com.jkd.utils.SpringUtil;
import lombok.extern.slf4j.Slf4j;
import org.nfunk.jep.JEP;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 金码测斜仪协议
 */
@Slf4j
public class JinMaCx implements EquipmentAgreement {
    /**
     * jep主要数据公式
     */
    private String FORMULA = "(a-48)*16+(b-48)+(c-48)*4096+(d-48)*256";
    /**
     * 协议帧最大字节数
     */
    public static final int MAX_FRAME_LEN = 20;
    /**
     * 协议帧正常响应最小字节数
     */
    public static final int MIN_FRAME_LEN = 26;
    /**
     * 帧结构中地址域的字节数
     */
    public static final int ADDRESS_FIELD_LEN = 6;
    /**
     * 帧起始符
     */
    public static final String FRAME_STARTER = "24";
    /**
     * 帧结束符
     */
    public static final String FRAME_END = "16";
    /**
     * 广播帧，当有新设备连接上来后服务端发送此指令和设备进行身份确认
     */
    public static final String BROADCAST_FRAME = "68 AA AA AA AA AA AA 68 11 04 33 33 33 33 AD 16";
    /**
     * 地址域：即设备的出厂编号，智能电表的编号一般在正面的二维码上
     */
    private String addressField;
    /**
     * 控制码
     */
    private String controlCode;
    /**
     * 数据域长度
     */
    private String dataLength;
    /**
     * 数据标识
     */
    private String dataIdentification;
    /**
     * 数据
     */
    private String data;
    /**
     * 校验码
     */
    private String checkSum;


    public JinMaCx() {
    }

    @Override
    public List<SourceData> analysis(String receiveDataStr, String sensorNum) {
        JEP jep = new JEP();
        String s = JkConvertUtil.hexStrToDecStr(receiveDataStr);
        String[] split = StrUtil.split(s, " ");
        jep.addVariable("a", Double.valueOf(split[1]));
        jep.addVariable("b", Double.valueOf(split[2]));
        jep.addVariable("c", Double.valueOf(split[3]));
        jep.addVariable("d", Double.valueOf(split[4]));
        jep.parseExpression(FORMULA);
        String e = "(x/10000-2.5)/4";
        jep.addVariable("x", jep.getValue());
        jep.parseExpression(e);
        double y = Math.toDegrees(jep.getValue());
        jep.addVariable("a", Double.valueOf(split[5]));
        jep.addVariable("b", Double.valueOf(split[6]));
        jep.addVariable("c", Double.valueOf(split[7]));
        jep.addVariable("d", Double.valueOf(split[8]));
        jep.parseExpression(FORMULA);
        jep.addVariable("x", jep.getValue());
        jep.parseExpression(e);
        double x = Math.toDegrees(jep.getValue());
        jep.addVariable("a", Double.valueOf(split[9]));
        jep.addVariable("b", Double.valueOf(split[10]));
        jep.addVariable("c", Double.valueOf(split[11]));
        jep.addVariable("d", Double.valueOf(split[12]));
        jep.parseExpression(FORMULA);
        jep.addVariable("x", jep.getValue());
        String wdgs = "(x-500)/10";
        jep.parseExpression(wdgs);
        double wd = jep.getValue();

        SourceData sourceData = new SourceData();
        sourceData.setSensorNum(sensorNum);
        sourceData.setDataOne(new BigDecimal(x).doubleValue());
        sourceData.setUnitOne("°");
        sourceData.setDataTwo(new BigDecimal(y).doubleValue());
        sourceData.setUnitTwo("°");
        sourceData.setTemperature(new BigDecimal(wd).doubleValue());
        sourceData.setAddTime(new Timestamp(new Date().getTime()));
        //判断报警
        MyAlarm myAlarm = (MyAlarm) SpringUtil.getBean("myAlarm");
        myAlarm.judgmentAlarm(sensorNum, x);
        log.info("倾斜数据存储完成，设备编号:"+sourceData.getSensorNum());
        return Arrays.asList(sourceData);
    }
}
